package com.t.bricks.bussiness.db.dao.synchtable.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.t.bricks.bussiness.db.dao.synchtable.SynchTableDao;
import com.t.bricks.bussiness.db.entity.synchtable.SynchTable;
import com.t.bricks.bussiness.db.mapper.synchtable.SynchTableMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class SynchTableDaoImpl implements SynchTableDao {

	@Autowired
	private SynchTableMapper synchTableMapper;

	/**
	 * 添加
	 * @param synchTable
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity add(SynchTable synchTable) {
		try {
			int i = synchTableMapper.add(synchTable);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "新增数据失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			return new MsgEmity(false, "新增失败！", 7002);
		}
		
		return new MsgEmity(true, "新增成功！", synchTable);
	}

	/**
	 * 删除
	 * @Param sTable
	 * @Param sDataId
	 * @Param sOwner
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity del(String sTable, String sDataId, String sOwner) {
		try {
			int i = synchTableMapper.del(sTable, sDataId, sOwner);
			if (i < 1) {
				Log.error("删除未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "删除数据失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("删除时发生异常:", e.getMessage());
			return new MsgEmity(false, "删除失败！", 7003);
		}
		
		return new MsgEmity(true, "删除成功！", 7999);
	}
	
	/**
	 * 清理指定用户数据
	 * @Param sOwner
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity clear(String sOwner) {
		try {
			int i = synchTableMapper.clear(sOwner);
			if (i < 1) {
				Log.error("清理指定用户数据未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "清理指定用户数据数据失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("清理指定用户数据时发生异常:", e.getMessage());
			return new MsgEmity(false, "清理指定用户数据失败！", 7003);
		}
		
		return new MsgEmity(true, "删除成功！", 7999);
	}

	/**
	 * 查询当前表最后的时间
	 * @Param sTable
	 * @Param sOwner
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findMaxDate(String sTable, String sOwner) {
		try {
			Date date = synchTableMapper.findMaxDate(sTable, sOwner);
			if (null == date) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
			
			return new MsgEmity(true, "查询成功！", date);
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
	}
	
	/**
	 * 查询当前表最后的时间(格式:格式:yyyy-MM-dd HH:mm:ss)
	 * @Param sTable
	 * @Param sOwner
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findMaxDateStr(String sTable, String sOwner) {
		try {
			String date = synchTableMapper.findMaxDateStr(sTable, sOwner);
			if (null == date) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
			
			return new MsgEmity(true, "查询成功！", date);
		} catch (Exception e) {
			Log.error("查询发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
	}

	/**
	 * 查询当前表最后的时间
	 * @Param sTable
	 * @Param sOwner
	 * @Param dDateSt
	 * @Param dDateEd
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByDate(String sTable, String sOwner, Date dDateSt, Date dDateEd) {
		List<SynchTable> list = null;
		try {
			list = synchTableMapper.findByDate(sTable, sOwner, dDateSt, dDateEd);
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("查询数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7003);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}
	
	/**
	 * 查询各用户最后同步时间
	 * @return
	 */
	@Override
	public MsgEmity findLastDate() {
		List<Map<String, String>> list = null;
		try {
			list = synchTableMapper.findLastDate();
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7002);
			}
		} catch (Exception e) {
			Log.error("查询数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7003);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}

}
